<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends HomeController {

    function __construct() { 
        parent::__construct();
        $this->load->model('Admin_model');
    }

    public function Index() {
        if(isset($_SESSION[SESS_USER_HOME]) && isset($_SESSION[SESS_USER_WX])){
            $data['detail']=$_SESSION[SESS_USER_HOME];
            $this -> load -> view('Home/Index',$data);
        }else{
            $this -> load -> view('Home/login');
        }
        
    }

    //使用api登陆
    function doLogin(){
        // var_dump($_POST);
        // exit();
        $param=$this->input->post();
        if(!($param['userinfo']))
        {
            exit(retJson('请先获取登陆信息', false));
        }
        $conner['cond']=array('usercode'=>$_POST['userinfo']['yhm'],'admin_type'=>AdminType::Auth[XPHEnum::Value]);
        $admininfo=$this->Admin_model->get_single_full($conner);
        if($admininfo)
        {
            if(!$admininfo['status'])
            {
                exit(retJson('该用户已被禁用！', false));
            }
            $update=array(
                'usercode'=>$_POST['userinfo']['yhm'],
                'username'=>$_POST['userinfo']['xm'],
                // 'role_id'=>WebRole::Teacher[XPHEnum::Value],
                'dept_id'=>$_POST['userinfo']['dwh'],
                'dept_name'=>$_POST['userinfo']['dwmc'],
                // ''=>time()
                );
            $this->Admin_model->update($update,'usercode');
        }
        else{
            $add=array(
                'usercode'=>$_POST['userinfo']['yhm'],
                'username'=>$_POST['userinfo']['xm'],
                // 'role_id'=>WebRole::Teacher[XPHEnum::Value],
                'dept_id'=>$_POST['userinfo']['dwh'],
                'dept_name'=>$_POST['userinfo']['dwmc'],
                'created'=>time(),
                'admin_type'=>AdminType::Auth[XPHEnum::Value]
                );
            if($_POST['userinfo']['lxdm']=='2'){
                $add['role_id']=WebRole::Teacher[XPHEnum::Value];
            }else if($_POST['userinfo']['lxdm']=='3'){
                $add['role_id']=WebRole::Student[XPHEnum::Value];
            }
            $this->Admin_model->add($add);
        }

        $_userinfo=$this->Admin_model->get_single(array('usercode'=>$_POST['userinfo']['yhm'],'admin_type'=>AdminType::Auth[XPHEnum::Value]));

        $_SESSION[SESS_USER_HOME]=$_userinfo;
        $arr=array(
            'id'=>$_userinfo['id'],
            'updated'=>time(),
            'ip'=>$_SERVER['REMOTE_ADDR']
        );
        $this->Admin_model->update($arr);
        exit(retJson('登录成功！', true));
    }
    
    //登陆本地数据库
    public function Login(){
        $this-> load ->model("Admin_model");        
        if(!isset($_POST['username'])){
            exit(retJson('请输入用户名',false));
        }
        if(!isset($_POST['pwd'])){
            exit(retJson('请输入密码',false));
        }
        $username = $_POST['username'];
        $pwd = $_POST['pwd'];
        $conner['items']='passsalt,status';
        $conner['where']=$this->sqlEscape(" usercode=?? and admin_type=?? ",array($username,AdminType::SelfAdmin[XPHEnum::Value]));        

        $userinfo=$this->Admin_model->get_single_full($conner);       
        //$result = $this -> User_model -> get_single(array("username" => $username));
        if($userinfo)
        {
            if(!$userinfo['status'])
            {
                exit(retJson('该用户已被禁用！', false));
            }
            $conner['items']='id,usercode,username,role_id,sub_role_id,phone,openid,status,admin_type,title,dept_id,dept_name,class_name,age_groups,teachers_attribute';
            $conner['cond']=array('usercode'=>$username,'password'=>getPasswrodWithTwiceEncode($pwd,$userinfo['passsalt']));
            $_userinfo=$this-> Admin_model ->get_single_full($conner);
            // var_dump($this->db->last_query());
            // exit();
            if($_userinfo)
            {
                $_SESSION[SESS_USER_HOME]=$_userinfo;
                    exit(retJson('登录成功！', true));
            }
            else{
                exit(retJson('帐号或者密码错误！', false));
            }
        }
        else{
            exit(retJson('不存在该用户！', false));
        }
    }
    

    	//湘云单点登录接口
	function ssoLogin(){
		$usercode = $_GET['usercode'];

		$connar['where'] = " admin_type = ".AdminType::Auth[XPHEnum::Value]." and (usercode='".$usercode."' or username='".$usercode."')";
		$_admininfo=$this->Admin_model->get_single_full($connar);
		
		if($_admininfo){
			if(!$_admininfo['status'])
			{
				echo "<h3>此系统该用户已被禁用！</h3>";
				
			}else{
                $_SESSION[SESS_USER_HOME]=$_admininfo;
				$arr=array(
					'id'=>$_admininfo['id'],
					'updated'=>time(),
					'ip'=>$_SERVER['REMOTE_ADDR']
				);
				$this->Admin_model->update($arr);
				redirect(site_url('Home/Index'));
			}

		}else{
			echo "<h3>此系统该用户不存在！</h3>";
		}

	
	}
    
}